<template>
  <div class="edit-cahnnels">
    <!-- 我的频道 -->
    <van-cell title="我的频道">
      <template #default>
        <van-button
          round
          size="mini"
          plain
          type="primary"
          @click="isEdit = !isEdit"
          >{{ isEdit ? "完成" : "编辑" }}</van-button
        >
      </template>
    </van-cell>
    <div class="my-channels">
      <div
        class="my-tags"
        v-for="(item, i) in channels"
        :key="item.id"
        @click="editChannels(i, item)"
        :class="{ current: i === active }"
      >
        <van-tag type="primary" round size="large" plain>
          {{ item.name }}
        </van-tag>
        <van-icon
          v-if="i !== 0"
          v-show="isEdit"
          name="clear"
          color="#ee0a24"
          size="16"
        />
      </div>
    </div>

    <!-- 推荐频道 -->
    <van-cell title="推荐频道"></van-cell>
    <div class="recommend-channels">
      <div
        class="my-tags"
        v-for="item in recommendChannels"
        :key="item.id"
        @click="addChannels(item)"
      >
        <van-tag type="primary" round size="large" plain>
          {{ item.name }}
        </van-tag>
      </div>
    </div>
  </div>
</template>

<script>
import { getAllChannels, addChannels } from '@/api/channels'
import { setChannel } from '@/utils/storage'
export default {
  name: 'EditChannels',
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  created () {
    this.getAllChannels()
  },
  computed: {
    recommendChannels () {
      return this.allChannels.filter((item) => {
        return !this.channels.some((item1) => item.id === item1.id)
      })
    }
  },
  methods: {
    // 获取所有频道
    async getAllChannels () {
      const { data } = await getAllChannels()
      console.log(data)
      this.allChannels = data.channels
    },
    // 添加频道
    async addChannels (item) {
      // this.channels.push(item);
      this.$emit('addChannel', item)
      if (this.$store.state.tokens.token) {
        // 登录添加频道
        try {
          await addChannels({
            id: item.id,
            seq: this.channels.length
          })
          this.$toast.success('添加频道成功')
        } catch (err) {
          this.$toast.fail('添加频道失败')
        }
      } else {
        // 未登录添加频道
        setChannel(this.channels)
      }
    },
    // 编辑频道
    editChannels (i, item) {
      if (!this.isEdit) {
        // 点击频道跳转
        this.$emit('changeChannels', i)
      } else {
        // 删除我的频道
        this.$emit('delCahnnels', item, i)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.edit-cahnnels {
  width: 100vw;
  padding: 50px 10px;
}
.van-button--mini {
  width: 44px;
  height: 22px;
  .van-button__text {
    font-size: 12px;
  }
}
.my-channels {
  display: flex;
  flex-wrap: wrap;
  .current {
    .van-tag--primary {
      color: red;
    }
  }
  .my-tags {
    position: relative;
    margin: 6px;
    .van-tag {
      padding: 10px 14px;
    }

    .van-icon {
      position: absolute;
      top: -5px;
      right: -2px;
    }
  }
}
.recommend-channels {
  display: flex;
  flex-wrap: wrap;
  .my-tags {
    position: relative;
    margin: 6px;
    .van-tag {
      padding: 10px 14px;
    }
  }
}
</style>
